/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package lab2;

import java.awt.Color;
import java.util.Random;
import javax.swing.border.LineBorder;


/**
 *
 * @author Максим
 */
public class SimplIns extends javax.swing.JFrame {

    private int arr[];
    private int currInt;
    private boolean redBorder[];
    private boolean blackBorder[];
    private int stage;
    private int currIndex;
    private int arrSize;
    private int sortIndex;
    /**
     * Creates new form SimplIns
     */
    public SimplIns() {
        initComponents();
        
        
    }
    public SimplIns(int arrSize) {
        initComponents();
        jButton2.setVisible(false);
        currInt = -1;
        sortIndex = -10;
        this.arrSize = arrSize;
        arr = new int[10];
        redBorder = new boolean[10];
        blackBorder = new boolean[10];
        /* clean array */
        for(int i = 0; i < 10; i++)
        {
            arr[i] = -10;
        }
        
        /* init random */
        Random randGen = new Random();
        
        for(int i = 0; i < arrSize; i++)
        {
            arr[i] = randGen.nextInt(100);
        }
        viewLabels();
        stage = 0;
        currIndex = 0;
        
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jL4 = new javax.swing.JLabel();
        currLabel = new javax.swing.JLabel();
        jL3 = new javax.swing.JLabel();
        jL2 = new javax.swing.JLabel();
        jL1 = new javax.swing.JLabel();
        jL10 = new javax.swing.JLabel();
        jL8 = new javax.swing.JLabel();
        jL7 = new javax.swing.JLabel();
        jL6 = new javax.swing.JLabel();
        jL9 = new javax.swing.JLabel();
        jL5 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();

        jButton1.setText("Следующий шаг");
        jButton1.setActionCommand("");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jL4.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N
        jL4.setText("31");

        currLabel.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N
        currLabel.setText("47");
        currLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));

        jL3.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N
        jL3.setText("66");

        jL2.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N
        jL2.setText("01");

        jL1.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N
        jL1.setText("07");

        jL10.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N
        jL10.setText("47");

        jL8.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N
        jL8.setText("66");

        jL7.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N
        jL7.setText("01");

        jL6.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N
        jL6.setText("07");

        jL9.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N
        jL9.setText("31");

        jL5.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N
        jL5.setText("47");

        jButton2.setText("Закрыть");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(currLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jL1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jL2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jL3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jL4)
                        .addGap(10, 10, 10)
                        .addComponent(jL5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jL6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jL7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jL8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jL9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jL10))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(381, 381, 381)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(currLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jL4)
                    .addComponent(jL3)
                    .addComponent(jL2)
                    .addComponent(jL1)
                    .addComponent(jL9)
                    .addComponent(jL8)
                    .addComponent(jL10)
                    .addComponent(jL7)
                    .addComponent(jL6)
                    .addComponent(jL5))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed
    void endSort()
    {
        jButton1.setVisible(false);
        jButton2.setVisible(true);
        
    }
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        // TODO add your handling code here:
        switch(stage)
        {
            /* get new current item*/
            case 0:
                
                if(currIndex < arrSize)
                {
                    redBorder[currIndex] = true;
                    viewLabels();
                    stage = 1;
                }
                else
                    endSort();
                
                break;
            /* put current item in current Label */
            case 1:
                currInt = arr[currIndex];
                redBorder[currIndex] = false;
                arr[currIndex] = -1;
                viewLabels();
                stage = 2;
                break;
            /* sorting */
            case 2:
                /* firt alvays sorted */
                if (currIndex == 0)
                {
                    arr[currIndex] = currInt;
                    currInt = -1;
                    blackBorder[currIndex] = true;
                    currIndex += 1;
                    stage = 0;
                    viewLabels();
                }
                else
                {
                    /* sort index get first time */
                    if (sortIndex == -10)
                        sortIndex = currIndex -1;
                    
                    if(sortIndex == -1)
                    {
                        arr[sortIndex + 1] = currInt;
                        currInt = -1;
                        blackBorder[sortIndex + 1] = true;
                        currIndex += 1;
                        stage = 0;
                        sortIndex = -10;
                        viewLabels();
                    }
                    if (arr[sortIndex] > currInt)
                    {
                        arr[sortIndex + 1] = arr[sortIndex];
                        arr[sortIndex] = -1;
                        blackBorder[sortIndex]= false;
                        blackBorder[sortIndex + 1]= true;
                        sortIndex += -1;
                        viewLabels();
                    }
                    else
                    {
                        arr[sortIndex + 1] = currInt;
                        currInt = -1;
                        blackBorder[sortIndex + 1] = true;
                        currIndex += 1;
                        stage = 0;
                        sortIndex = -10;
                        viewLabels();
                    }
                
                }
                break;
            default:
                
        }
    }//GEN-LAST:event_jButton1MouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton2MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SimplIns.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SimplIns.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SimplIns.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SimplIns.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SimplIns().setVisible(true);
            }
        });
    }

    private void viewLabels()
    {
        jL1.setVisible(false);
        jL2.setVisible(false);
        jL3.setVisible(false);
        jL4.setVisible(false);
        jL5.setVisible(false);
        jL6.setVisible(false);
        jL7.setVisible(false);
        jL8.setVisible(false);
        jL9.setVisible(false);
        jL10.setVisible(false);
        currLabel.setVisible(false);
        
        if(currInt != -1)
        {
            currLabel.setText(fillLabel(currInt));
            currLabel.setVisible(true);
        }
        
        if (arr[0] != -1)
            jL1.setText(fillLabel(arr[0]));
        else
            jL1.setText("__");
        
        if (blackBorder[0])
                jL1.setBorder(new LineBorder(Color.black));
        else if (redBorder[0])
                jL1.setBorder(new LineBorder(Color.red));
        else jL1.setBorder(null);
        jL1.setVisible(true);
        
        
        
        if (arr[1] != -1)
            jL2.setText(fillLabel(arr[1]));
        else
            jL2.setText("__");
        
        if (blackBorder[1])
            jL2.setBorder(new LineBorder(Color.black));
        else if (redBorder[1])
            jL2.setBorder(new LineBorder(Color.red));
        else jL2.setBorder(null);
        
        jL2.setVisible(true);
        
        if (arr[2] != -1)
            jL3.setText(fillLabel(arr[2]));
        else
            jL3.setText("__");
        
        if (blackBorder[2])
            jL3.setBorder(new LineBorder(Color.black));
        else if (redBorder[2])
            jL3.setBorder(new LineBorder(Color.red));
        else jL3.setBorder(null);
        
        jL3.setVisible(true);
        
        if (arr[3] != -1)
            jL4.setText(fillLabel(arr[3]));
        else
            jL4.setText("__");
        
        if (blackBorder[3])
            jL4.setBorder(new LineBorder(Color.black));
        else if (redBorder[3])
            jL4.setBorder(new LineBorder(Color.red));
        else jL4.setBorder(null);
        
        jL4.setVisible(true);
        
        if (arr[4] != -1)
            jL5.setText(fillLabel(arr[4]));
        else
            jL5.setText("__");
        
        if (blackBorder[4])
            jL5.setBorder(new LineBorder(Color.black));
        else if (redBorder[4])
            jL5.setBorder(new LineBorder(Color.red));
        else jL5.setBorder(null);
        
        if(arr[4] != -10)
            jL5.setVisible(true);
        
        if (arr[5] != -1)
            jL6.setText(fillLabel(arr[5]));
        else
            jL6.setText("__");
        
        if (blackBorder[5])
            jL6.setBorder(new LineBorder(Color.black));
        else if (redBorder[5])
            jL6.setBorder(new LineBorder(Color.red));
        else jL6.setBorder(null);
        
        if(arr[5] != -10)
            jL6.setVisible(true);
        
        if (arr[6] != -1)
            jL7.setText(fillLabel(arr[6]));
        else
            jL7.setText("__");
        
        if (blackBorder[6])
            jL7.setBorder(new LineBorder(Color.black));
        else if (redBorder[6])
            jL7.setBorder(new LineBorder(Color.red));
        else jL7.setBorder(null);
        
        if(arr[6] != -10)
            jL7.setVisible(true);
        
        if (arr[7] != -1)
            jL8.setText(fillLabel(arr[7]));
        else
            jL8.setText("__");
        
        if (blackBorder[7])
            jL8.setBorder(new LineBorder(Color.black));
        else if (redBorder[7])
            jL8.setBorder(new LineBorder(Color.red));
        else jL8.setBorder(null);
        
        if(arr[7] != -10)
            jL8.setVisible(true);
        
        if (arr[8] != -1)
            jL9.setText(fillLabel(arr[8]));
        else
            jL9.setText("__");
        
        if (blackBorder[8])
            jL9.setBorder(new LineBorder(Color.black));
        else if (redBorder[8])
            jL9.setBorder(new LineBorder(Color.red));
        else jL9.setBorder(null);
        
        if(arr[8] != -10)
            jL9.setVisible(true);
        
        
        if (arr[9] != -1)
            jL10.setText(fillLabel(arr[9]));
        else
            jL10.setText("__");
        
        if (blackBorder[9])
            jL10.setBorder(new LineBorder(Color.black));
        else if (redBorder[9])
            jL10.setBorder(new LineBorder(Color.red));
        else jL10.setBorder(null);
        
        if(arr[9] != -10)
            jL10.setVisible(true);
        
        
    }
    
    String fillLabel(int num)
    {
        if (num < 10)
            return "0" + Integer.toString(num);
        else
            return Integer.toString(num);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel currLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jL1;
    private javax.swing.JLabel jL10;
    private javax.swing.JLabel jL2;
    private javax.swing.JLabel jL3;
    private javax.swing.JLabel jL4;
    private javax.swing.JLabel jL5;
    private javax.swing.JLabel jL6;
    private javax.swing.JLabel jL7;
    private javax.swing.JLabel jL8;
    private javax.swing.JLabel jL9;
    // End of variables declaration//GEN-END:variables
}
